package ink.lovejinhu.leetcode;

import java.util.HashMap;
import java.util.Set;

/**
 * @author jinhu
 * created 2021-07-14 9:36
 */
public class Solution_0387 {
    /**
     * @param s
     * @return
     */
    public int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        int firstLoaction = Integer.MAX_VALUE;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i])) {
                map.put(chars[i], Integer.MAX_VALUE);
            } else {
                map.put(chars[i], i);
            }
        }
        Set<Character> characters = map.keySet();
        for (char temp :characters
        ) {
            if (map.get(temp) == Integer.MAX_VALUE) {
                continue;
            } else {
                if (map.get(temp) < firstLoaction) firstLoaction = map.get(temp);
            }

        }

        return firstLoaction == Integer.MAX_VALUE ? -1 : firstLoaction;
    }

    public static void main(String[] args) {
        Solution_0387 solution_0387 = new Solution_0387();
        solution_0387.firstUniqChar("leetcode");
        System.out.println(solution_0387.firstUniqChar("leetcode"));
    }
}
